package com.lazason.wib4android;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.lazason.wib4android.AsyncImageLoader.ImageCallback;
 

public class ImageBrowserActivity extends Activity {
	String original_pic;
	String id;
	AsyncImageLoader asyncImageLoader;
	 ImageView originalPicture;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.d("ImageBrowserActivity", "onCreate");
		setContentView(R.layout.imagebrowser);
		Log.d("ImageBrowserActivity", "setContentView");
		id = this.getIntent().getStringExtra("id");
		original_pic = this.getIntent().getStringExtra("original_pic");
		asyncImageLoader = new AsyncImageLoader(); 
		originalPicture = (ImageView)findViewById(R.id.originalImage);
		Log.d("ImageBrowserActivity", "findViewById");
		originalPicture.setScaleType(ImageView.ScaleType.CENTER);
		   originalPicture.setTag(id);
		   originalPicture.setImageResource(R.drawable.icon);
		   
		   Log.d("ImageBrowserActivity", "setImageResource");
		   Drawable cachedImage = asyncImageLoader
					.loadDrawable(this, original_pic, id,
							Thread.MAX_PRIORITY,
							new ImageCallback() {
								public void imageLoaded(
										Drawable imageDrawable,
										String imageUrl,
										String id) {
									ImageView imageViewByTag = (ImageView) originalPicture.findViewWithTag(id);
									if (imageViewByTag != null
											&& imageDrawable != null) { // 防止图片url获取不到图片是，占位图片不见了的情况
										imageViewByTag
												.setImageDrawable(imageDrawable);

									}
								}
							});
		   Log.d("ImageBrowserActivity", "loadDrawable");
			if (cachedImage != null) {
				originalPicture.setImageDrawable(cachedImage);
			}
			
			
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		
		if(originalPicture.getDrawable()!=null){
			if(originalPicture.getDrawable() instanceof BitmapDrawable){
				BitmapDrawable bd = (BitmapDrawable)originalPicture.getDrawable();
				originalPicture.setImageDrawable(null);
				bd.setCallback(null);
				if(bd.getBitmap()!=null){
				bd.getBitmap().recycle();
				}
			}
		}
		
		super.onDestroy();
		
	}

}
